package views.components;

import utils.NavigatorUtils;
import utils.SessionUtils;

import com.vaadin.annotations.Theme;
import com.vaadin.event.LayoutEvents.LayoutClickEvent;
import com.vaadin.event.LayoutEvents.LayoutClickListener;
import com.vaadin.event.MouseEvents.ClickEvent;
import com.vaadin.server.ThemeResource;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.Image;
import com.vaadin.ui.Label;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

import config.Parameters;

@SuppressWarnings("serial")
@Theme("Theme02")
/**
 * Clase que representa un link a una aplicacio
 *
 */
public class AppLink extends CustomComponent {

	private VerticalLayout root, subContenedor, layoutLbls;
	private GridLayout grid;
	private Image img;
	private Label lblAppName, lblDescription;
	private String viewName, idApp;

	public AppLink(String iconName, String appName, String appDescription,
			final String idApp, final String viewName, boolean novice) {

		this.viewName = viewName;
		this.idApp = idApp;
		img = new Image("", new ThemeResource(Parameters.ICO_APP_PATH
				+ iconName));
		lblAppName = new Label(appName);
		lblDescription = new Label(appDescription);

		iniciarVariables();
		ficarEstils();
		lblDescription.setVisible(novice);
		lligarComponents();
	}

	private void ficarEstils() {
		lblAppName.addStyleName("LINK_FONT cursor_POINTER background_bisque");
		lblDescription
				.addStyleName("LINK_FONT1 cursor_POINTER background_bisque");
		layoutLbls.addStyleName("cursor_POINTER background_bisque");
		layoutLbls.addStyleName("HEADER_TITLE3");
		subContenedor
				.setStyleName("cursor_POINTER background_bisque link_margins ");
		root.addStyleName("link_margins");
		lblDescription.setContentMode(ContentMode.HTML);
		subContenedor.setWidth("240px");
		grid.setMargin(true);
		grid.setSpacing(true);
	}

	private void iniciarVariables() {
		root = new VerticalLayout();
		subContenedor = new VerticalLayout();
		layoutLbls = new VerticalLayout();
		grid = new GridLayout(2, 2);

		subContenedor.addLayoutClickListener(new LayoutClickListener() {
			@Override
			public void layoutClick(LayoutClickEvent event) {

				SessionUtils.setSessionCurrentApp(idApp);

				try {
					NavigatorUtils.navigateTo(UI.getCurrent().getNavigator(),
							viewName);
				} catch (Exception e) {
					Notification.show(e.getLocalizedMessage());
					e.printStackTrace();
				}
			}
		});
	}

	private void lligarComponents() {
		layoutLbls.addComponents(lblAppName, lblDescription);
		grid.addComponent(img, 0, 0);
		grid.addComponent(layoutLbls, 1, 0);
		grid.setComponentAlignment(img, Alignment.TOP_LEFT);
		grid.setComponentAlignment(layoutLbls, Alignment.MIDDLE_CENTER);
		subContenedor.addComponent(grid);
		root.addComponent(subContenedor);
		setCompositionRoot(root);
	}

	public void setNovice(boolean novice) {
		lblDescription.setVisible(novice);
	}

	public void buttonClick(ClickEvent event) {

		SessionUtils.setSessionCurrentApp(idApp);
		try {
			NavigatorUtils.navigateTo(UI.getCurrent().getNavigator(), viewName);
		} catch (Exception e) {
			Notification.show(e.getMessage());
		}
	}

}
